package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.TradeDetailInfoMapper;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.TxnCfg;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易明细信息正式表接口类
 * Created by yuhongwen
 */
public class TradeDetailInfoMapperImpl implements TradeDetailInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);


    @Override
    public List<MchntOrderInfo> selectMchntOrder(String instId, String mchntNo, String txnDate, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrder");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntOrderInfo.setMchntNo(mchntNo);
        }
        if (null != userId && !userId.isEmpty()) {
            mchntOrderInfo.setUserId(userId);
        }
        if (null != txnDate && !txnDate.isEmpty()) {
            mchntOrderInfo.setTxnDate(txnDate);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<PayOrderInfo> selectPayOrder(String instId, String mchntOrderId, String payType, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrder");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        if (null != mchntOrderId && !mchntOrderId.isEmpty()) {
            payOrderInfo.setMchntOrderId(mchntOrderId);
        }
        if (null != payType && !payType.isEmpty()) {
            payOrderInfo.setPayType(payType);
        }
        if (null != sysSeqNum && !sysSeqNum.isEmpty()) {
            payOrderInfo.setSysSeqNum(sysSeqNum);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectMchntOrderPage(String instId, String mchntNo, String startDate, String endDate, String userId, PageInfo pageInfo, String txnNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderPage");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntOrderInfo.setMchntNo(mchntNo);
        }
        if (null != userId && !userId.isEmpty()) {
            mchntOrderInfo.setUserId(userId);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntOrderInfo.setTxnDate(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntOrderInfo.setRemark1(endDate);
        }
        if (null != txnNum && !txnNum.isEmpty()) {
            mchntOrderInfo.setTxnNum(txnNum);
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class, pageInfo);
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderGroupByDate(String instId, String mchntNo, String startDate, String endDate, String userId, String transType,
                                                            String payType, String txnNum, String minAmt, String maxAmt, String mchntOrderId, String mchntType, String queryId
    ) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderGroupByDate");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {

            if ("00".equals(mchntType)) { //普通商户或门店
                mchntOrderInfo.setMchntNo(mchntNo);
            } else if ("10".equals(mchntType)) {//集团
                mchntOrderInfo.setRealSettleType(mchntNo);
            } else if ("20".equals(mchntType)) { //平台
                mchntOrderInfo.setRealSettleFlag(mchntNo);
            } else if ("30".equals(mchntType)) { //连锁商户
                mchntOrderInfo.setRealSettleStatus(mchntNo);

            }
        }
        if (null != userId && !userId.isEmpty()) {
            mchntOrderInfo.setUserId(userId);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntOrderInfo.setTxnDate(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntOrderInfo.setRemark1(endDate);
        }
        if (null != txnNum && !txnNum.isEmpty()) {
            mchntOrderInfo.setTxnNum(txnNum);
        }
        mchntOrderInfo.setRemark3(payType);
        mchntOrderInfo.setOrderExtraInfo(queryId);

        /**正向交易*/
        if ("P01".equals(transType)) {
            mchntOrderInfo.setRemark2(transType);
            /**反向交易*/
        } else if ("P04".equals(transType)) {
            mchntOrderInfo.setBizType(transType);
        }
        mchntOrderInfo.setOrderAmt(minAmt);
        mchntOrderInfo.setFeeCappingAmt(maxAmt);
        mchntOrderInfo.setMchntOrderId(mchntOrderId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectPayOrderPage(String instId, String mchntNo, String startDate, String endDate, String userId, PageInfo pageInfo, String payType, String minAmt, String maxAmt, String mchntOrderId,
                                         String transType, String state, String mchntType, String queryId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderPage");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            if ("00".equals(mchntType)) { //普通商户或门店
                mchntOrderInfo.setMchntNo(mchntNo);
            } else if ("10".equals(mchntType)) {//集团
//                mchntOrderInfo.setRealSettleType(mchntNo);
            } else if ("20".equals(mchntType)) { //平台
//                mchntOrderInfo.setRealSettleFlag(mchntNo);
            } else if ("30".equals(mchntType)) { //连锁商户
                mchntOrderInfo.setChainMchntNo(mchntNo);
            }

        }
        if (null != userId && !userId.isEmpty()) {
            mchntOrderInfo.setUserId(userId);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntOrderInfo.setTxnDate(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntOrderInfo.setRemark1(endDate);
        }
        if (null != state && !state.isEmpty()) {
            mchntOrderInfo.setTransStatus(state);
        }
        mchntOrderInfo.setStageNum(queryId);

        mchntOrderInfo.setOrderAmt(minAmt);
        mchntOrderInfo.setRemark2(maxAmt);
        mchntOrderInfo.setMchntOrderId(mchntOrderId);
        mchntOrderInfo.setRemark3(transType);

        return dbsUtil.dbsFetchPageListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class, pageInfo);
    }

    @Override
    public List<PayOrderInfo> selectPayOrderByMchntNoAndtransStatus(String instId, String mchntOrderId, String transStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderByMchntNoAndtransStatus");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        if (null != mchntOrderId && !mchntOrderId.isEmpty()) {
            payOrderInfo.setMchntOrderId(mchntOrderId);
        }
        if (null != transStatus && !transStatus.isEmpty()) {
            payOrderInfo.setTransStatus(transStatus);
        }
        payOrderInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> bspsSelectPayOrderInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bspsSelectPayOrderInfoBySysSeqNum");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据系统流水号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());

    }

    @Override
    public List<MchntOrderInfo> selectCashWithdrawalMchntOrderGroupByDate(String instId, String mchntNo, String startDate, String endDate, String userId, String transType, String payType, String txnNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCashWithdrawalMchntOrderGroupByDate");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntOrderInfo.setMchntNo(mchntNo);
        }
        if (null != userId && !userId.isEmpty()) {
            mchntOrderInfo.setUserId(userId);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntOrderInfo.setTxnDate(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntOrderInfo.setRemark1(endDate);
        }
        if (null != txnNum && !txnNum.isEmpty()) {
            mchntOrderInfo.setTxnNum(txnNum);
        }
        mchntOrderInfo.setRemark3(payType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectPayOrderDownload(String instId, String mchntNo, String startDate, String endDate, String userId, String payType, String minAmt, String maxAmt, String mchntOrderId,
                                                       String transType, String state, String mchntType, String queryId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderPage");
        MchntOrderInfo payOrderInfo = new MchntOrderInfo();
        payOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            if ("00".equals(mchntType)) { //普通商户或门店
                payOrderInfo.setMchntNo(mchntNo);
            } else if ("10".equals(mchntType)) {//集团
                //payOrderInfo.setCustomerFee(mchntNo);
            } else if ("20".equals(mchntType)) { //平台
                //payOrderInfo.setChannelFee(mchntNo);
            } else if ("30".equals(mchntType)) { //连锁商户
                payOrderInfo.setChainMchntNo(mchntNo);
            }
        }

        if (null != userId && !userId.isEmpty()) {
            payOrderInfo.setUserId(userId);
        }

        if (null != startDate && !startDate.isEmpty()) {
            String startDates = startDate.substring(0, 8);
            payOrderInfo.setTxnDate(startDates);
            if (startDate.length() == 12) {
                String startTime = startDate.concat("00");
                payOrderInfo.setCreateTime(startTime);
            }
            if (startDate.length() == 14) {
                payOrderInfo.setCreateTime(startDate);
            }
        }

        if (null != endDate && !endDate.isEmpty()) {
            String endDates = endDate.substring(0, 8);
            payOrderInfo.setRemark1(endDates);

            if (endDate.length() == 12) {
                String endTime = endDate.concat("60");
                payOrderInfo.setUpdateTime(endTime);
            }
            if (endDate.length() == 14) {
                payOrderInfo.setUpdateTime(endDate);
            }
        }

        /*if (null != payType && !payType.isEmpty()) {
            payOrderInfo.setPayType(payType);
        }*/
        if (null != state && !state.isEmpty()) {
            payOrderInfo.setTransStatus(state);
        }
        payOrderInfo.setStageNum(queryId);

        payOrderInfo.setOrderAmt(minAmt);
        payOrderInfo.setRemark2(maxAmt);
        payOrderInfo.setMchntOrderId(mchntOrderId);
        payOrderInfo.setRemark3(transType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<TxnCfg> selectTxnCfgByBizType(String txnNum, String bizType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnCfgByBizType");
        TxnCfg txnCfg = new TxnCfg();
        txnCfg.setTxnNum(txnNum);
        txnCfg.setBizType(bizType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnCfg, TxnCfg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据业务细分查询交易配置信息失败,txnNum:<{}>,bizType:<{}>", txnNum, bizType);
            return null;
        }
        return objectList.stream().map(item -> (TxnCfg) item).collect(Collectors.toList());
    }

}
